using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;
using WeBlog.Interfaces;

namespace WeBlog.Models
{
    public class Post : IModel
    {
        [Key]
    	public int PostId { get; set; }

    	[Required]        
        public int AuthorId { get; set; }
        [ForeignKey("AuthorId")]
        public virtual User Author { get; set; }

    	[Required]
        [StringLength(255)]
    	public string Title { get; set; }
    	
        [StringLength(4000)]
    	public string Description { get; set; }
    	
        [Required]
        [MaxLength]
        public string Content { get; set; }
    	
        [Required]
    	public bool AreCommentsEnabled { get; set; }
        
        [DataType(DataType.Date)]
        public DateTime DateCreated { get; set; }
        public DateTime? PublishDate { get; set; }    	
    	
        [Required]
    	public DateTime LastModified { get; set; }    	
    	
        [Required]
    	public bool Featured { get; set; }    	
    	
        [Required]
    	public bool Published { get; set; }    	
    	
        [StringLength(255)]
    	public string Slug { get; set; }                	

    	public virtual ICollection<PostVisit> PostVisits { get; set; }

        public virtual ICollection<Tag> Tags { get; set; }

        public virtual ICollection<PostRating> PostRatings { get; set; }

        public virtual ICollection<Category> Categories { get; set; }
        
        public virtual ICollection<PostComment> PostComments { get; set; }        

        public string GetSummary(int length = 200)
        {
            string text;
            if (!String.IsNullOrWhiteSpace(Description))
            {
                text = Description;
            }
            else
            {
                text = StripHtml(Content);
                if (text.Length > length)
                    text = text.Substring(0, length) + " ...";
                text = "\"" + text.Trim() + "\"";
            }
            return text;
        }

        private readonly Regex _regex = new Regex("<[^>]*>", RegexOptions.Compiled);
        /// <summary>
        /// Removes all HTML tags from a given string
        /// </summary>
        private string StripHtml(string html)
        {
            if (string.IsNullOrEmpty(html))
                return string.Empty;

            return _regex.Replace(html, string.Empty);
        }

        public Post()
        {
            DateCreated = DateTime.Now;
            PostVisits = new List<PostVisit>();
            PostComments = new List<PostComment>();
            PostRatings = new List<PostRating>();
            Categories = new List<Category>();
            Tags = new List<Tag>();
        }
        
        public IEnumerable<PostComment> ApprovedComments
        {
            get { return PostComments.Where(x => x.CommentStatus == CommentStatuses.Approved).OrderBy(x => x.DateTime); }
        }
    }
}
